var $ = require('../../../lib/jquery');
var util = require('./util');
var template = require('../../tpl/template');
var nav = function () {
   this.init();
}
nav.prototype.init = function () {
  var _this = this;
  util.fetch('api/service/list',{
    timeout:10000
  }).then(function (res){
  _this.render(res.data);    
  })
  this.hide()
}
nav.prototype.id = null;
nav.isshow = false;
nav.prototype.render = function (data) {
  this.data = data;
  var tpl = template('nav', {
    data: data
  });
  $('.js_nav').html(tpl);
  this.bindEvent()
}
nav.prototype.hide = function() {
  this.isshow = false;
  $('.js_sub_nav').hide();
  $('.js_nav li').removeClass('active').data({
    active: 0
  });
}
nav.prototype.bindEvent= function(){
  var _this = this;
  $('body').on('click', '.js_nav li', function () {
     var id = $(this).data().id;
     var active = $(this).data().active;
     if(id=='index'){
      if (location.href.indexOf('index') == -1){
        location.href= './index.html';
      }
     }else if (id && !active) {
      _this.id = id;
       $(this).siblings().removeClass('active');
       $(this).addClass('active');
       $(this).parent().find('li').data({
         active: 0
       });
       $(this).data({
         active:1
       })
       _this.sub_nav(id);
    }else{
      _this.hide();
    }
  })
  $('.page').on('click',function (e) {
    // console.log($(e.target).closest('.js_nav').length ,$(e.target).closest('.js_sub_nav').length);
    if (!$(e.target).closest('.js_nav').length && !$(e.target).closest('.js_sub_nav').length) {
      _this.hide();
    }
  })
  this.subEvent()
}
nav.prototype.subEvent= function() {
  var _this = this;
   $('.js_sub_nav').on('click', 'li', function () {
     _this.hide();
     var id= $(this).data().id,
     order = $(this).data().order,
     parentid = $(this).data().parentid,
     siteurl = $(this).data().siteurl;
     if (!!siteurl){
      location.href = siteurl;
     }else if (id == 14 && order == 1 && parentid == 2) {
       location.href = './jyqy.html?id='+id+'&order='+order+'&parentid='+parentid
     } else if (id == 32 && order == 5 && parentid == 5) { // 常用电话
        location.href= './phone.html';
     } else if (id == 10 && order == 5 && parentid == 1){ //健康体检
      location.href='./jktj.html'
     } else if (id == 13 && order == 8 && parentid == 1) { // 地图
      location.href = './map.html';
     } else if (id == 20 && order == 3 && parentid == 3) { // 预防接种
      location.href='./yfjz.html'
     } else if (id == 6 && order == 1 && parentid == 1){ // 只能导诊
      location.href = './zndz.html';
     } else if (id == 6 && order == 1 && parentid == 1){ // 
      location.href= './zytz.html';
     } else if (id == 16 && order == 3 && parentid == 2) {
      location.href = './zwjc.html'
     } else if (id == 15 && order == 2 && parentid == 2) { // 中医提示辨识
      location.href = './zytz.html';
     } else if (id == 7 && order == 2 && parentid == 1) {
       location.href = './yygh.html'
     } else if (id == 12 && order == 7 && parentid == 1) { // 急诊指南
      location.href='./jzzn.html'
     } else if (id == 26 && order == 4 && parentid == 4) {
      location.href = './szjg.html' // 辅助生殖机构查询
     } else if (id == 17 && order == 4 && parentid == 2) {
      location.href ='./mbgl.html'
     } else if (id == 25 && order == 3 && parentid == 4) {
       location.href = './jhsy.html'
     } else if (id == 11 && order == 6 && parentid == 1) {
      location.href = './health.html'
    } else if (id == 9 && order == 4 && parentid == 1) {
      location.href = './bgxq.html'
    } else {
       alert('ui稿尚未提供', JSON.stringify($(this).data()))
     }
   })
}
nav.prototype.getsubdata= function (id) {
  var s = [];
  (this.data || []).forEach(function(item){
    if(item.id == id){
      s.push(item)
    }
  })
  return s;
}
nav.prototype.sub_nav = function (id){
    var data = this.getsubdata(id);
    var tpl = template('sub_nav',{data:data[0],id:id});
    $('.js_sub_nav').html(tpl).show();
    this.isshow = true;
}
nav.prototype.getparam = function () {
}
$('.js_index').on('click', function(){
  location.href = "./index.html";
})

module.exports = nav;